<?php

class CostController extends Controller
{
	
	public function actionIndex()
	{
            $this ->registerExtjs();
            
            $this->render('index',array(

            ));
	}
        
        public function actionGetList()
	{
            list($costList, $total) = Cost::model() -> getList();
            
            echo json_encode(array('costs' => $costList, 'total' => $total));
	}
        
        public function actionUpdate()
	{
            if(isset($_POST['id']) && $_POST['id'] != "")
            {
                $cost =  Cost::model() ->findByPk($_POST['id']);
                $cost -> title = $_POST['title'];
                $cost -> type = $_POST['type'];
                $cost -> value = $_POST['value'];
                
                if($cost -> update())
                    echo 'true';
                else
                    echo 'false';
            }
            else
            {
                $cost = new Cost();
                $cost -> title = $_POST['title']; 
                $cost -> type = $_POST['type'];
                $cost -> value = $_POST['value'];
                
                if($cost -> save())
                    echo 'true';
                else
                    echo 'false';
            }            
	}
        
        public function actionDelete()
	{
            if(isset($_POST['id']))
            {
                if(Cost::model() ->deleteByPk($_POST['id']) > 0)
                    echo 'true';
                else
                    echo 'false';
            }
            
            echo '';
	}
}
